// pages/distribution/distribution_center.js
const api = require("../../request/api")
const request = require("../../request/request")
const util = require("../../utils/util")
const App = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    uid: '',
    ctype: 0,
    page: 1,
    pageSize: 10,
    avatarUrl: 'https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132',
    nickName: '点击显示微信头像',
    if_register: false,
    eshop_id: '',
    user_list: [],
    shopdetail_data: {},

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.init_data(options)
    this.get_shopcustomer()
    this.get_shopdetail()
  },

  get_shopdetail() {
    let params = {
      id: this.data.eshop_id,
    }
    request.post(api.shopdetail, params).then(res => {
      this.setData({
        shopdetail_data: res,
      })
    })
  },

  get_shopcustomer() {
    let params = {
      id: this.data.eshop_id,
    }
    request.post(api.shopcustomer, params).then(res => {
      this.setData({
        user_list: res
      })
    })
  },

  init_data(options) {
    let eshop_id = App.globalData.eshop_id
    let uid = util.getAppValue(App.globalData.uid)
    if (!uid) {
      uid = 1
    }
    this.setData({
      if_register: App.globalData.if_register,
      uid,
      eshop_id,
      options,
    })
    let avatarUrl = App.globalData.userInfo.avatarUrl
    let nickName = App.globalData.userInfo.nickName
    if (avatarUrl) {
      this.setData({
        avatarUrl,
        eshop_id,
        nickName
      })
    }
  },

  check_register() {
    if (!this.data.if_register) {
      this.get_user_profile()
    }
  },

  get_user_profile() {
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: res => {
        App.globalData.userInfo = res.userInfo
        App.globalData.if_register = true
        this.init_data(this.data.options)
        // let pages = getCurrentPages()
        // let prev_page = pages[pages.length - 2]
        // prev_page.onLoad()
        // util.goBack()
      },
      fail: res => {
        console.log(res);
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    let pageSize = this.data.pageSize
    pageSize += 10
    this.setData({
      pageSize
    })

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})